<%@ page import="dao.ProductDao" %>
<%@ page import="domain.Product" %><%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2021/12/25
  Time: 13:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
    <title>后台-修改商品反馈</title>
</head>
<body>
<%@include file="headForRoot.jsp"%>
<%
    String modifyString = request.getParameter("modify");
    if (modifyString==null){
%>
<script>
    alert("您没有指定要修改的产品。是怎么导航到此页面的？");
    window.location="manage.jsp";
</script>
<%
    }else{
        String pname = request.getParameter("pname").equals("")?null:request.getParameter("pname");
        String quantity = request.getParameter("quantity").equals("")?null:request.getParameter("quantity");
        String price = request.getParameter("price").equals("")?null:request.getParameter("price");
        String desc = request.getParameter("desc").equals("")?null:request.getParameter("desc");
        if (pname==null&&quantity==null&&price==null&&desc==null){
            out.print("<div style=\"text-align: center\">您没有做任何修改，三秒后将为您重新跳转至后台管理页面……</div>");
            response.setHeader("refresh","3;URL=manage.jsp");
        }else{
            ProductDao dao = new ProductDao();
            boolean legal = true;
            // 需要对用户输入数据的合法性进行检测
            if (pname!=null&&dao.exist(pname)){
                legal = false;
%>
<script type="text/javascript">
    alert("很抱歉，您改写的商品名称已经存在。商品重名不被允许。");
    window.location="manage.jsp";
</script>
<%
            }
            if (legal){
                try {
                    int modify = Integer.parseInt(modifyString);
                    Product origin = dao.search(modify);
                    Product modified = new Product();
                    modified.setName(pname==null?origin.getName():pname);
                    modified.setCount(quantity==null?origin.getCount():Integer.parseInt(quantity));
                    modified.setPrice(price==null?origin.getPrice():Double.parseDouble(price));
                    modified.setDescription(desc==null?origin.getDescription():desc);
                    if (dao.modify(modify,modified)){
%>
<div style="font-size: larger;color: chocolate;text-align: center;">修改商品信息成功！</div><br />
<div style="text-align: center">
    以下商品信息：<br />
    <table style="margin-left: 35%">
        <tr>
            <td style="width: 100px">商品名：</td>
            <td><%=origin.getName()%></td>
        </tr>
        <tr>
            <td>商品库存：</td>
            <td><%=origin.getCount()%></td>
        </tr>
        <tr>
            <td>商品价格：</td>
            <td><%=origin.getPrice()%></td>
        </tr>
        <tr>
            <td>商品描述：</td>
            <td><%=origin.getDescription()%></td>
        </tr>
    </table>
    <br />已经被修改为：<br />
    <table style="margin-left: 35%">
        <tr>
            <td style="width: 100px">商品名：</td>
            <td><%=modified.getName()%></td>
        </tr>
        <tr>
            <td>商品库存：</td>
            <td><%=modified.getCount()%></td>
        </tr>
        <tr>
            <td>商品价格：</td>
            <td><%=modified.getPrice()%></td>
        </tr>
        <tr>
            <td>商品描述：</td>
            <td><%=modified.getDescription()%></td>
        </tr>
    </table>
    <br /><br />
    五秒后将为您重新跳转至后台管理页面……
<%
                        response.setHeader("refresh","5;URL=manage.jsp");
                    }
                }catch (NumberFormatException e){
                    e.printStackTrace();
                }
            }
        }
    }
%>
<br />
<%@include file="tail.jsp"%>
</body>
</html>
